package com.tarena.bo;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.tarena.util.HbnUtils;
import com.tarena.vo.Dept;

/**
 * 对数据库中的部门表操作类
 * 
 * @author WangPeihu
 * 
 */
public class DeptDao {

	/**
	 * 获得全部的部门信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Dept> findAllDept() {
		Session s = HbnUtils.getSession();
		Query query = s.createQuery("from Dept");
		List<Dept> deptList = query.list();
		s.close();
		return deptList;
	}

	/**
	 * 增加一个部门
	 * 
	 * @param dept
	 */
	public void insertDept(Dept dept) {
		Session s = HbnUtils.getSession();
		try {
			s.beginTransaction();
			s.save(dept);
			s.getTransaction().commit();
		} catch (HibernateException e) {
			s.getTransaction().rollback();
			throw new RuntimeException(e);
		} finally {
			s.close();
		}
	}
}